package com.sysmind.ravi.margin.schedulerFiles;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.sysmind.ravi.margin.model.Stock;

public class ExtractStockInfofromFile 
{	
	public List<Stock> getStockList()
	{	List<String> stockData = new ArrayList<String>();
		List<Stock> stockList=null;

		FileReader fr;
	
		try 
		{	
			fr = new FileReader("stockData.txt");
	
			BufferedReader br = new BufferedReader(fr); 
			String s; 		
	
			while((s = br.readLine()) != null)
			{		
				// this statement reads the line from the file and print it to
				// the console.
				//System.out.println("hello"+s);
				stockData.add(s);			      
			}		

			stockList = populateStockList(stockData);
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		return stockList;
	}
	
	
	public static List<Stock> populateStockList(List<String> stockData)
	{	Iterator iterator1;
		List<Stock> returnStockList = new ArrayList<Stock>();
		
		for(iterator1 = stockData.iterator(); iterator1.hasNext();)
		{	
			//System.out.println(iterator.next()+" "+(i++)+" ");
			returnStockList.add(extractStockInfo(iterator1.next().toString()));
		}			
		
		/*
		for(iterator1 = returnStockList.iterator(); iterator1.hasNext();)
		{
			System.out.println(iterator1.next().toString());
		}
		*/
		return returnStockList;
	}

	public static Stock extractStockInfo(String oneStockData)
	{	Stock returnStock = new Stock();
		
		StringBuilder sb = new StringBuilder(oneStockData);
		//System.out.println(sb.toString());
		
		returnStock.setStockId(extractStockId(sb));		
		//System.out.println(returnStock.getStockId());
		
		returnStock.setStockSymbol(extractStockSymbol(sb));
		//System.out.println(returnStock.getStockSymbol());
		
		returnStock.setStockPrice(extractStockPrice(sb));
		//System.out.println(returnStock.getStockPrice());
		
		returnStock.setStockTimeStamp(extractStockTimeStamp(sb));
		//System.out.println(returnStock.getStockTimeStamp());
		
		returnStock.setStockType(extractStockType(sb));
		//System.out.println(returnStock.getStockType());
		
		return returnStock;
	}

	
	public static int extractStockId(StringBuilder sb)
	{	int i;
		//Lines of Code to extract Stock ID
		i = sb.indexOf("id");
		sb = sb.delete(0, i);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf(":");
		sb = sb.delete(0, i);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf("\"");
		sb = sb.delete(0,i+1);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf("\"");
		return (Integer.valueOf((sb.toString().substring(0, i))));
		
		//return 0;		
	}

	public static String extractStockSymbol(StringBuilder sb)
	{	int i = 0;
		
		//System.out.println(sb.toString());
	
		i = sb.indexOf("t");
		sb = sb.delete(0,i+1);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf(":");
		sb = sb.delete(0, i+3);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf("\"");
		//System.out.println(i + sb.toString());
		
		return ((sb.toString().substring(0, i)));
		//return null;		
	}

	public static double extractStockPrice(StringBuilder sb)
	{	int i = 0;
	
		//System.out.println(sb.toString());
	
		i = sb.indexOf("l");
		sb = sb.delete(0,i+1);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf(":");
		sb = sb.delete(0, i+3);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf("\"");
		//System.out.println(i + sb.toString());
		
		return ((Double.valueOf(sb.toString().substring(0, i))));
				
		//return 0.0;
	}

	@SuppressWarnings("deprecation")
	public static long extractStockTimeStamp(StringBuilder sb)
	{	Date date = Calendar.getInstance().getTime();
		
		//System.out.println(date.getTime()+" "+date.getHours()+" "+date.getMinutes()+" "+date.getYear()+" "+date.getDate()+" "+date.getMonth()+" "+date.getYear());
		
		return date.getTime();
		/*
		int i;
		System.out.println(sb.toString());
		
		i = sb.indexOf("lt");
		sb = sb.delete(0,i+1);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf(":");
		sb = sb.delete(0, i+3);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf("lt");
		sb = sb.delete(0,i+1);
		//System.out.println(i+sb.toString());
		
		i = sb.indexOf(":");
		sb = sb.delete(0, i+3);
		//System.out.println(i+sb.toString());
		
		String dateString = sb.substring(0,sb.indexOf("\"")).toString();
		System.out.println(dateString);
		
		return 0;
		*/
	}

	public static int extractStockType(StringBuilder sb)
	{	int i = 0;
	
		i = sb.indexOf("Stock Type");
		sb = sb.delete(0,i+10);
		//System.out.println(i+sb.toString());
		
		//System.out.println(sb.substring(1, 2));
		
		return Integer.valueOf(sb.substring(1, 2));
	}

}
